#include "Camera.h"

Camera::Camera(RenderWindow &app) {
    // create copy of default window view
    view = View(app.GetDefaultView().GetRect());
    
    // setup other variables
    target = view.GetCenter();
    halfSize = view.GetHalfSize();
    currZoom = 1.0f;
    targetZoom = 1.0f;
}

Camera::~Camera() {
}

View &Camera::GetView() {
    return view;
}

void Camera::SetTarget(Vector2f &t) {
    target = t;
}

void Camera::SetTarget(float x, float y) {
    Vector2f t(x,y);
    SetTarget(t);
}

void Camera::MoveTarget(float x, float y) {
    target += Vector2f(x, y);
}

void Camera::Zoom(float offset) {
    SetZoom(targetZoom+offset);
}

void Camera::SetZoom(float zoom) {
    targetZoom = zoom;
    
    // cap zooming
    if (targetZoom < ZOOM_MIN) targetZoom = ZOOM_MIN;
    else if (targetZoom > ZOOM_MAX) targetZoom = ZOOM_MAX;
}

void Camera::Update(float dt) {
    // move view position by a fraction of difference
    Vector2f offset = target-view.GetCenter();
    offset *= 10*dt;
    view.Move(offset);

    // change zoom by fraction of difference
    currZoom += (targetZoom-currZoom)*5*dt;
    view.SetHalfSize(halfSize/currZoom);
}

